package com.mall.db.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.OmsOrderItemMapper;
import com.mall.db.mapper.OmsOrderMapper;
import com.mall.db.mapper.OmsOrderOperateHistoryMapper;
import com.mall.dto.OmsOrderDto;
import com.mall.pojo.*;
import com.mall.vo.OmsOrderVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class OmsOrderApiImpl implements OmsOrderApi {

    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;

    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;


    /**
     * 批量删除订单功能
     *
     * @param ids
     * @return
     */
    @Override
    public Integer deleteOrder(List<Integer> ids) {
        //设置删除条件
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ObjectUtil.isNotEmpty(ids), OmsOrder::getId, ids);
        Integer deleteNum = omsOrderMapper.delete(queryWrapper);
        return deleteNum;
    }

    /**
     * 批量关闭订单信息
     * @param ids
     * @param note
     * @return
     */
    @Override
    public Integer updateCloseStatus(List<Integer> ids, String note) {
       //设置修改条件


        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrder::getId,ids);
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setStatus(4);
        //修改状态码
        Integer updateNum = omsOrderMapper.update(omsOrder, queryWrapper);

        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
        //同时修改备注信息
        List<OmsOrder> omsOrders = omsOrderMapper.selectList(queryWrapper);
        for (OmsOrder order : omsOrders) {
            omsOrderOperateHistory.setOrderId(order.getId());
            omsOrderOperateHistory.setCreateTime(new Date());
            omsOrderOperateHistory.setOperateMan("后台管理员");                 //假数据
            omsOrderOperateHistory.setOrderStatus(order.getStatus());
            omsOrderOperateHistory.setNote("订单关闭:"+note);
            omsOrderOperateHistoryMapper.insert(omsOrderOperateHistory);
        }

        return updateNum;

    }

    /**
     * 跟据id查看订单列表详细信息
     *
     * @param id
     * @return
     */
    @Override
    public OmsOrderVo findOrderById(Integer id) {
        //先获取order表中的数据
       OmsOrder omsOrder= omsOrderMapper.selectById(id);
        //获取查询条件在orderitem表中获取相关数据
        LambdaQueryWrapper<OmsOrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, OmsOrderItem::getOrderId, id);
        List<OmsOrderItem> omsOrderItems = omsOrderItemMapper.selectList(queryWrapper);
        //获取在表OmsOrderOperateHistory中的数据
        LambdaQueryWrapper<OmsOrderOperateHistory> qw = new LambdaQueryWrapper<>();
        qw.eq(id!=null,OmsOrderOperateHistory::getOrderId,id);
        List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderOperateHistoryMapper.selectList(qw);

        //封装数据
        OmsOrderVo omsOrderVo = new OmsOrderVo();

        BeanUtil.copyProperties(omsOrder,omsOrderVo);

        omsOrderVo.setOrderItemList(omsOrderItems);
        omsOrderVo.setHistoryList(omsOrderOperateHistories);

        return omsOrderVo;
    }

    /**
     * 修改备注信息
     * @param id
     * @param note
     * @param status
     * @return
     */
    @Override
    public Integer updateNoteById(Long id, String note, Integer status) {
//        调用方法进行查询
        OmsOrder omsOrder = omsOrderMapper.selectById(id);
        omsOrder.setNote(note);
        omsOrder.setStatus(status);
        //设置查询条件
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrder::getId,id);
        //调用方法进行修改
        Integer updateNum = omsOrderMapper.update(omsOrder, queryWrapper);
        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
        omsOrderOperateHistory.setOrderId(id);
        omsOrderOperateHistory.setCreateTime(new Date());
         omsOrderOperateHistory.setOperateMan("后台管理员");                 //假数据
        omsOrderOperateHistory.setOrderStatus(status);
        omsOrderOperateHistory.setNote("修改备注信息:"+note);
         omsOrderOperateHistoryMapper.insert(omsOrderOperateHistory);


        return updateNum;
    }



    /**
     * 批量发货
     * @param deliveryParamList
     * @return
     */
    @Override
    public Integer updateDeliveryByIds(List<OmsOrderDeliveryParam> deliveryParamList) {

        Integer updateNum = 0;
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();

        //先进行遍历
        for (OmsOrderDeliveryParam ooDp : deliveryParamList) {
            //查询条件
            queryWrapper.eq(OmsOrder::getId,ooDp.getOrderId());

            //根据id获取到正确的OmsOrder
            OmsOrder omsOrder = omsOrderMapper.selectById(ooDp.getOrderId());
            //封装数据
            omsOrder.setDeliveryCompany(ooDp.getDeliverCompany());
            omsOrder.setStatus(2);
            omsOrder.setDeliverySn(ooDp.getDeliverySn());
            //封装完成后调用方法进行修改
             updateNum = omsOrderMapper.update(omsOrder, queryWrapper);


            //对历史操作信息进行修改

            omsOrderOperateHistory.setOrderId(omsOrder.getId());
            omsOrderOperateHistory.setCreateTime(new Date());
            omsOrderOperateHistory.setOperateMan("后台管理员");                 //假数据
            omsOrderOperateHistory.setOrderStatus(omsOrder.getStatus());
            omsOrderOperateHistory.setNote("完成发货");
            omsOrderOperateHistoryMapper.insert(omsOrderOperateHistory);

        }


return updateNum;
    }


    /**
     * 修改用户收货信息
     *
     * @param receiverInfoParam
     * @return
     */
    @Override
    public Integer updateReceiverInfo(OmsReceiverInfoParam receiverInfoParam) {
        //设置查询条件
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrder::getId,receiverInfoParam.getOrderId());
        //封装数据
        OmsOrder omsOrder = omsOrderMapper.selectById(receiverInfoParam.getOrderId());
        omsOrder.setReceiverCity(receiverInfoParam.getReceiverCity());
        omsOrder.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        omsOrder.setReceiverName(receiverInfoParam.getReceiverName());
        omsOrder.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        omsOrder.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        omsOrder.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        omsOrder.setStatus(receiverInfoParam.getStatus());

        //调用方法对数据进行修改
        Integer updateNum = omsOrderMapper.update(omsOrder, queryWrapper);
        //对历史操作信息进行修改

        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
        omsOrderOperateHistory.setOrderId(omsOrder.getId());
        omsOrderOperateHistory.setCreateTime(new Date());
        omsOrderOperateHistory.setOperateMan("后台管理员");                 //假数据
        omsOrderOperateHistory.setOrderStatus(omsOrder.getStatus());
        omsOrderOperateHistory.setNote("修改收货人信息");
        omsOrderOperateHistoryMapper.insert(omsOrderOperateHistory);

        return updateNum;
    }

    /**
     * 订单列表
     * 分页查询功能
     *
     * @param omsOrderDto
     * @return
     */
    @Override
    public Page<OmsOrder> omsOrderPage(OmsOrderDto omsOrderDto) {
        Page<OmsOrder> page = new Page<>(omsOrderDto.getPageNum(), omsOrderDto.getPageSize());
        //添加查询条件
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        //订单提交时间
        queryWrapper.eq(omsOrderDto.getCreateTime() != null, OmsOrder::getCreateTime, omsOrderDto.getCreateTime());
        //订单编号
        queryWrapper.eq(omsOrderDto.getOrderSn() != null, OmsOrder::getOrderSn, omsOrderDto.getOrderSn());
        //订单类型
        queryWrapper.eq(omsOrderDto.getOrderType() != null, OmsOrder::getOrderType, omsOrderDto.getOrderType());
        //收货人姓名/号码
        queryWrapper.like(omsOrderDto.getReceiverKeyWord() != null, OmsOrder::getReceiverName, omsOrderDto.getReceiverKeyWord());

        queryWrapper.like(omsOrderDto.getReceiverKeyWord() != null, OmsOrder::getReceiverPhone, omsOrderDto.getReceiverKeyWord());
        //订单来源
        queryWrapper.eq(omsOrderDto.getSourceType() != null, OmsOrder::getSourceType, omsOrderDto.getSourceType());
        //订单状态
        queryWrapper.eq(omsOrderDto.getStatus() != null, OmsOrder::getStatus, omsOrderDto.getStatus());

        //调用mapper方法
        Page<OmsOrder> pageRe = omsOrderMapper.selectPage(page, queryWrapper);

        return pageRe;
    }


}
